/*
 * IntegrationHelper.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.actions.system;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import org.tiberiumlabs.lailaps.actions.AbstractResourcedAction;
import org.tiberiumlabs.lailaps.gui.elements.ExceptionDialog;
import org.tiberiumlabs.lailaps.settings.ApplicationSettings;
import org.tiberiumlabs.swing.DesktopToolkit;

/**
 *
 * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class ViewLogAction extends AbstractResourcedAction {

    public ViewLogAction() {
        // TODO add to resources
        putValue(NAME, "View Log");
        putValue(SHORT_DESCRIPTION, "View Log");
        putValue(LONG_DESCRIPTION, "View Log");
        //putValue(LARGE_ICON_KEY, ImageLoader.getSettingsIcon());
        //putValue(SMALL_ICON, ImageLoader.getMenuSettings());
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {

            File logFile = new File(ApplicationSettings.LOG_FILE);
            BufferedReader reader = new BufferedReader(new FileReader(logFile));
            char[] logContent = new char[(int)logFile.length()];
            int read = reader.read(logContent);
            reader.close();

            JTextArea textArea = new JTextArea(32, 80);
            textArea.setEditable(false);
            textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 11));
            textArea.setText(new String(logContent, 0, read));

            JScrollPane scrollPane = new JScrollPane(textArea);

            JPanel contentPane = new JPanel(new BorderLayout());
            int borderSize = 12;
            contentPane.setBorder(BorderFactory.createEmptyBorder(borderSize, borderSize, borderSize, borderSize));
            contentPane.setDoubleBuffered(true);
            contentPane.add(scrollPane, BorderLayout.CENTER);

            // we dont need to check for visibility of main window here, because we cant even invoke this action without main window visible.
            final JDialog dialog = new JDialog(mediator.getGuiBuilder().getMainWindow(), "Log file", false);
            dialog.setContentPane(contentPane);

            dialog.getRootPane().registerKeyboardAction(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dialog.dispose();
                }
            }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);

            dialog.pack();
            dialog.setLocation(DesktopToolkit.getWindowLocation(dialog.getSize()));

            dialog.setVisible(true);

        } catch (Exception ex) {
            new ExceptionDialog(ex);
        }
    }

}
